/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-10 15:29:26  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.bp.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.google.gson.Gson;
import com.joysuch.wwyt.bp.bean.BpRiskPromiseBraodcastListBean;
import com.joysuch.wwyt.bp.bean.BpRiskPromiseBraodcastListBean.Search;
import com.joysuch.wwyt.bp.bean.MhRiskBroadcastBean;
import com.joysuch.wwyt.bp.entity.BpRiskPromiseBraodcast;
import com.joysuch.wwyt.bp.service.BpRiskPromiseBraodcastService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "风险研判-承诺公告")
@RequestMapping("/bp/risk-promise-braodcast")
public class BpRiskPromiseBraodcastController {

    private static final Logger logger = LoggerFactory.getLogger(BpRiskPromiseBraodcastController.class);

    @Autowired
    private BpRiskPromiseBraodcastService bpRiskPromiseBraodcastService;

    @RequestMapping("add")
    public Object add(@Valid @RequestBody MhRiskBroadcastBean entity) {
        try {
            bpRiskPromiseBraodcastService.addOrUpdate(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @RequestMapping("update")
    public Object update(@Valid @RequestBody MhRiskBroadcastBean entity) {
        try {
            bpRiskPromiseBraodcastService.addOrUpdate(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败", e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @RequestMapping("today")
    public Object today() {
        try {
            return ResultBean.success(bpRiskPromiseBraodcastService.getTodayBroadcast());
        } catch (Exception e) {
            logger.error("查询今日公告失败", e);
            return ResultBean.fail(101, "查询今日公告失败");
        }
    }

    @RequestMapping("template")
    public Object template() {
        try {
            return ResultBean.success(bpRiskPromiseBraodcastService.loadTemplate());
        } catch (Exception e) {
            logger.error("查询今日公告失败", e);
            return ResultBean.fail(101, "查询今日公告失败");
        }
    }

    @RequestMapping("page")
    public Object page(Pageable page, Search condition) {
        try {
            page = PageDataUtils.addOrderByDesc(page, "date");
            Page<BpRiskPromiseBraodcastListBean> data = bpRiskPromiseBraodcastService.findByPage(page, condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            bpRiskPromiseBraodcastService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        BpRiskPromiseBraodcast broadcast = bpRiskPromiseBraodcastService.findById(id);
        return ResultBean.success(new Gson().fromJson(broadcast.getContent(), MhRiskBroadcastBean.class));
    }
}
